package org.hackystat.projectbrowser.page.chooseTAP;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.link.Link;
import org.hackystat.ambientdevice.server.jaxb.Project;
import org.hackystat.ambientdevice.server.jaxb.TriggerActionPair;
import org.hackystat.projectbrowser.ProjectBrowserSession;

/**
 * The link that deals with deleting TriggerActionPairs.
 * @author Sonwright M. Gomez
 * @author Ed Kim
 *
 */
public class DeleteTriggerActionPairLink extends Link {

  private List<TriggerActionPair> pairs = new ArrayList<TriggerActionPair>();
  private String projectName;
  private String triggerActionPairId;
  private Project project;

  /**
   * Constructor
   * @param id the ID.
   * @param triggerActionPairId the tap id.
   * @param projectName the project Name.
   */
  public DeleteTriggerActionPairLink(String id, String triggerActionPairId, String projectName) {
    super(id);
    this.projectName = projectName;
    this.triggerActionPairId = triggerActionPairId;
    this.project = ProjectBrowserSession.get().getProjectTypeXMLByNameId(this.projectName);
    pairs = project.getTriggerActionPairs().getTriggerActionPair();
  }

  /**
   * Supports Serialization.
   */
  private static final long serialVersionUID = 1L;

  /**
   * link to new page
   */
  @Override
  public void onClick() {
    List<TriggerActionPair> toDelete = new ArrayList<TriggerActionPair>();
    for (TriggerActionPair temp : pairs) {
      if (temp.getId().equals(this.triggerActionPairId)) {
        toDelete.add(temp);
      }
    }
    pairs.removeAll(toDelete);

  }

}
